package com.fifth.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Banner;
import com.fifth.common.pojo.entity.HeadNav;
import com.fifth.common.pojo.entity.Zone;
import com.fifth.home.mapper.BannerMapper;
import com.fifth.home.service.IBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Resource
    private BannerMapper bannerMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 前端轮播
     *
     * @return
     */
    @Override
    public CommonResult selectRotational() {
        if (redisTemplate.hasKey("banner")) {
            //查看redis是否存在banner
            List<Banner> list = JSON.parseObject(redisTemplate.opsForValue().get("banner"), List.class);
            return new CommonResult(2000, "轮播查询成功", list);

        } else {
            QueryWrapper<Banner> wrapper = new QueryWrapper<>();
            wrapper.eq("is_disable", 0);
            wrapper.eq("is_deleted", 0);
            wrapper.orderByAsc("sort");
            List<Banner> list = bannerMapper.selectList(wrapper);

            String s = JSON.toJSONString(list);
            //存入redis
            redisTemplate.opsForValue().set("banner", s,24, TimeUnit.HOURS);
            return new CommonResult(2000, "轮播查询成功", list);
        }
    }

    /**
     * 轮播 模糊查询
     *
     * @param banner
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult fuzzyRotationQuery(Banner banner, Integer currentPage, Integer pageSize) {
        IPage<Banner> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(banner.getTitle()), "title", banner.getTitle());
        wrapper.eq(StringUtils.isNotBlank(banner.getIsDisable()), "is_disable", banner.getIsDisable());
        wrapper.eq("is_deleted", "0");
        wrapper.orderByAsc("id");

        IPage<Banner> zoneIPage = bannerMapper.selectPage(page, wrapper);

        return new CommonResult(2000, "查询成功", zoneIPage);
    }

    /**
     * 添加轮播图
     * @param banner
     * @return
     */
    @Override
    public CommonResult insertBanner(Banner banner) {
        int insert = bannerMapper.insert(banner);

        String banner1 = redisTemplate.opsForValue().get("banner");
        if (banner1 != null && !"".equals(banner1)) {
            Boolean banner2 = redisTemplate.delete("banner");
        }
        return new CommonResult(2000, "添加轮播成功", null);
    }

    /**
     * 修改轮播状态
     * @param id
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult updateStatus(String id, String isDisable) {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Banner banner = new Banner();
        banner.setIsDisable(isDisable);
        int update = bannerMapper.update(banner, wrapper);

        String banner1 = redisTemplate.opsForValue().get("banner");
        if(banner1!=null && !"".equals(banner1)){
            //删除redis数据
            Boolean head1 = redisTemplate.delete("banner");
        }
        return new CommonResult(2000, "修改状态成功", null);
    }

    /**
     * 删除轮播
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteBanner(String id) {
        int i = bannerMapper.deleteById(id);

        String banner = redisTemplate.opsForValue().get("banner");
        if(banner!=null && !"".equals(banner)){
            Boolean banner1 = redisTemplate.delete("banner");
        }
        return new CommonResult(2000, "删除轮播成功", null);
    }

    /**
     * 修改轮播信息
     * @param banner
     * @return
     */
    @Override
    public CommonResult updateBanner(Banner banner) {
        int update = bannerMapper.updateById(banner);

        String banner1 = redisTemplate.opsForValue().get("banner");
        if(banner1!=null && !"".equals(banner1)){
            Boolean banner2 = redisTemplate.delete("banner");
        }
        return new CommonResult(2000, "修改轮播信息成功", null);
    }
}
